/*
 *   Copyright (c) 2010-2022, SIUFUNG
 */
package net.siufung.market.base.user.controller.assembler;

import com.google.common.collect.Lists;
import net.siufung.core.utils.CopyUtil;
import net.siufung.core.utils.StringPool;
import net.siufung.market.base.user.mapper.entity.UserDetail;
import net.siufung.market.base.user.api.dto.UserDeptDto;
import net.siufung.market.base.user.controller.model.vo.UserExcelVo;
import net.siufung.market.base.user.controller.model.vo.UserVo;
import net.siufung.market.base.user.mapper.entity.User;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 *
 * 系统用户表 持久层展示层组装器
 *
 * @author 陈建峰
 * @since 2022-07-05 16:14:50
 */
public class UserAssembler {

    /**
     * User 转换成 UserVo
     * @param user 数据库实体
     * @return UserVo 视图
     */
    public static UserVo from(User user){
        if(!Objects.isNull(user)){
            UserVo userVo = new UserVo();
            CopyUtil.copyProperties(user, userVo);
            userVo.setLoginPwd(StringPool.EMPTY);
            return userVo;
        }
        return null;
    }

    /**
     * User 转换成 UserVo
     * @param user 数据库实体
     * @param userDetail 数据库实体
     * @return UserVo 视图
     */
    public static UserVo from(User user, UserDetail userDetail){
        if(!Objects.isNull(user)){
            UserVo userVo = new UserVo();
            CopyUtil.copyProperties(user, userVo);
            userVo.setLoginPwd(StringPool.EMPTY);
            CopyUtil.copyProperties(userDetail, userVo);
            return userVo;
        }
        return null;
    }

    /**
     * UserList 转换成 UserVoList
     * @param userList 数据库实体集合
     * @return List<UserVo> 视图集合
     */
    public static List<UserVo> fromList(List<User> userList){
        List<UserVo> userVoList = Lists.newArrayList();
        if(!CollectionUtils.isEmpty(userList)){
            UserVo userVo;
            for (User user: userList) {
                userVo = new UserVo();
                CopyUtil.copyProperties(user, userVo);
                userVo.setLoginPwd(StringPool.EMPTY);
                userVoList.add(userVo);
            }
        }
        return userVoList;
    }

    /**
     * UserList 转换成 UserVoList
     * @param userList 数据库实体集合
     * @param userDeptDtoList 组织Dto集合
     * @return List<UserVo> 视图集合
     */
    public static List<UserVo> fromList(List<User> userList, List<UserDeptDto> userDeptDtoList){
        List<UserVo> userVoList = Lists.newArrayList();
        if(!CollectionUtils.isEmpty(userList)){
            UserVo userVo;
            for (User user: userList) {
                userVo = new UserVo();
                CopyUtil.copyProperties(user, userVo);
                userVo.setLoginPwd(StringPool.EMPTY);
                for (UserDeptDto deptApiDto: userDeptDtoList) {
                    if(user.getDeptId().equals(deptApiDto.getDeptId())){
                        userVo.setDeptName(deptApiDto.getDeptName());
                    }
                }
                userVoList.add(userVo);
            }
        }
        return userVoList;
    }

    /**
     * UserList 转换成 UserVoList
     * @param userList 数据库实体集合
     * @param userDetailList 数据库实体集合
     * @param userDeptDtoList 组织Dto集合
     * @return List<UserVo> 视图集合
     */
    public static List<UserVo> fromList(List<User> userList, List<UserDetail> userDetailList,
                                        List<UserDeptDto> userDeptDtoList){
        List<UserVo> userVoList = Lists.newArrayList();
        if(!CollectionUtils.isEmpty(userList)){
            UserVo userVo;
            for (User user: userList) {
                userVo = new UserVo();
                CopyUtil.copyProperties(user, userVo);
                userVo.setLoginPwd(StringPool.EMPTY);
                for (UserDeptDto deptApiDto: userDeptDtoList) {
                    if(Objects.nonNull(user.getDeptId()) &&
                            user.getDeptId().equals(deptApiDto.getDeptId())){
                        userVo.setDeptName(deptApiDto.getDeptName());
                    }
                }
                for (UserDetail userDetail: userDetailList) {
                    if(user.getId().intValue() == userDetail.getUserId().intValue()){
                        CopyUtil.copyProperties(userDetail, userVo);
                    }
                }
                userVoList.add(userVo);
            }
        }
        return userVoList;
    }


    public static List<UserExcelVo> fromListForExcel(List<User> userList){
        List<UserExcelVo> userExcelVoList = Lists.newArrayList();
        if(!CollectionUtils.isEmpty(userList)){
            UserExcelVo userExcelVo;
            for (User user: userList) {
                userExcelVo = new UserExcelVo();
                CopyUtil.copyProperties(user, userExcelVo);
                userExcelVoList.add(userExcelVo);
            }
        }
        return userExcelVoList;
    }

}

